﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Nop.Core.Domain.Common;
using Nop.Services.Customers;
using Nop.Services.Helpers;
using Nop.Services.Localization;
using Nop.Services.ShareBuy;
using Nop.Core.Domain.ShareBuy;
using Nop.Services.Security;
using Nop.Web.Framework;
using Nop.Web.Framework.Controllers;
using Telerik.Web.Mvc;
using Nop.Admin.Models.ShareBuy;

namespace Nop.Admin.Controllers
{
	[AdminAuthorize]
    public class ShareBuyController : BaseNopController
	{
		#region Fields

        private readonly IShareBuyService _shareBuyService;
        private readonly ILanguageService _languageService;
        private readonly IDateTimeHelper _dateTimeHelper;
        private readonly ICustomerContentService _customerContentService;
        private readonly ILocalizationService _localizationService;
        private readonly IPermissionService _permissionService;
        private readonly AdminAreaSettings _adminAreaSettings;
        
		#endregion

		#region Constructors

        public ShareBuyController(IShareBuyService shareBuyService, ILanguageService languageService,
            IDateTimeHelper dateTimeHelper, ICustomerContentService customerContentService,
            ILocalizationService localizationService, IPermissionService permissionService,
            AdminAreaSettings adminAreaSettings)
        {
            this._shareBuyService = shareBuyService;
            this._languageService = languageService;
            this._dateTimeHelper = dateTimeHelper;
            this._customerContentService = customerContentService;
            this._localizationService = localizationService;
            this._permissionService = permissionService;
            this._adminAreaSettings = adminAreaSettings;
		}

		#endregion 
        
        #region News items

        public ActionResult Index()
        {
            return RedirectToAction("List");
        }

        public ActionResult List()
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageNews))
                return AccessDeniedView();

            var shares = _shareBuyService.GetShareBuys(0, null, 0, _adminAreaSettings.GridPageSize, true);
            var gridModel = new GridModel<ShareBuyItemModel>
            {
                Data = shares.Select(x =>
                {
                    var m = x.ToModel();
                    m.CreatedOnUtc = _dateTimeHelper.ConvertToUserTime(x.CreatedOnUtc, DateTimeKind.Utc);
                    m.Comments = x.ShareBuyComments.Count;
                    return m;
                }),
                Total = shares.TotalCount
            };
            return View(gridModel);
        }

        [HttpPost, GridAction(EnableCustomBinding = true)]
        public ActionResult List(GridCommand command)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageNews))
                return AccessDeniedView();

            var news = _shareBuyService.GetShareBuys(0, null, command.Page - 1, command.PageSize, true);
            var gridModel = new GridModel<ShareBuyItemModel>
            {
                Data = news.Select(x =>
                {
                    var m = x.ToModel();
                    m.CreatedOnUtc = _dateTimeHelper.ConvertToUserTime(x.CreatedOnUtc, DateTimeKind.Utc);
                    m.Comments = x.ShareBuyComments.Count;
                    return m;
                }),
                Total = news.TotalCount
            };
            return new JsonResult
            {
                Data = gridModel
            };
        }

        [HttpPost]
        public ActionResult Delete(int id)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageNews))
                return AccessDeniedView();

            var shareBuyItem = _shareBuyService.GetShareBuyById(id);
            if (shareBuyItem == null)
                //No shareBuyItem item found with the specified id
                return RedirectToAction("List");

            _shareBuyService.DeleteShareBuy(shareBuyItem);

            SuccessNotification("Sharebuy deleted");
            //return RedirectToAction("List");
            return Content("Success");
        }
        
        #endregion

        #region Comments

        //public ActionResult Comments(int? filterByNewsItemId)
        //{
        //    if (!_permissionService.Authorize(StandardPermissionProvider.ManageNews))
        //        return AccessDeniedView();

        //    ViewBag.FilterByNewsItemId = filterByNewsItemId;
        //    var model = new GridModel<NewsCommentModel>();
        //    return View(model);
        //}

        //[HttpPost, GridAction(EnableCustomBinding = true)]
        //public ActionResult Comments(int? filterByNewsItemId, GridCommand command)
        //{
        //    if (!_permissionService.Authorize(StandardPermissionProvider.ManageNews))
        //        return AccessDeniedView();

        //    IList<NewsComment> comments;
        //    if (filterByNewsItemId.HasValue)
        //    {
        //        filter comments by news item
        //        var newsItem = _newsService.GetNewsById(filterByNewsItemId.Value);
        //        comments = newsItem.NewsComments.OrderBy(bc => bc.CreatedOnUtc).ToList();
        //    }
        //    else
        //    {
        //        load all news comments
        //        comments = _customerContentService.GetAllCustomerContent<NewsComment>(0, null);
        //    }

        //    var gridModel = new GridModel<NewsCommentModel>
        //    {
        //        Data = comments.PagedForCommand(command).Select(newsComment =>
        //        {
        //            var commentModel = new NewsCommentModel();
        //            commentModel.Id = newsComment.Id;
        //            commentModel.NewsItemId = newsComment.NewsItemId;
        //            commentModel.NewsItemTitle = newsComment.NewsItem.Title;
        //            commentModel.CustomerId = newsComment.CustomerId;
        //            commentModel.IpAddress = newsComment.IpAddress;
        //            commentModel.CreatedOn = _dateTimeHelper.ConvertToUserTime(newsComment.CreatedOnUtc, DateTimeKind.Utc);
        //            commentModel.CommentTitle = newsComment.CommentTitle;
        //            commentModel.CommentText = Core.Html.HtmlHelper.FormatText(newsComment.CommentText, false, true, false, false, false, false);
        //            return commentModel;
        //        }),
        //        Total = comments.Count,
        //    };
        //    return new JsonResult
        //    {
        //        Data = gridModel
        //    };
        //}

        //[GridAction(EnableCustomBinding = true)]
        //public ActionResult CommentDelete(int? filterByNewsItemId, int id, GridCommand command)
        //{
        //    if (!_permissionService.Authorize(StandardPermissionProvider.ManageNews))
        //        return AccessDeniedView();

        //    var comment = _customerContentService.GetCustomerContentById(id);
        //    if (comment == null)
        //        throw new ArgumentException("No comment found with the specified id");
        //    _customerContentService.DeleteCustomerContent(comment);

        //    return Comments(filterByNewsItemId, command);
        //}


        #endregion
    }
}
